"""TODO header"""

import logging

from google.appengine.ext import db
from google.appengine.ext.db import polymodel


APP_CONFIG_ENTITY_KEY_NAME = 'hot_potato_app_config'

def get_app_config():
  """Gets the app config from the datastore."""
  app_config = HotPotatoAppConfig.get_by_key_name(APP_CONFIG_ENTITY_KEY_NAME)
  if app_config is None:
    app_config = _make_default_app_config()
  return app_config

def _make_default_app_config():
  from config.field.dropdown_field import DropdownField
  logging.info('No app config found. Creating default.')
  app_config = HotPotatoAppConfig(key_name=APP_CONFIG_ENTITY_KEY_NAME)
  app_config.put()
  DropdownField(app_config=app_config, field_name='first_field').put()
  DropdownField(app_config=app_config, field_name='second_field').put()
  return app_config


class HotPotatoAppConfig(db.Model):
  """Model for the app configuration

  This model stores stores app-wide properties of the submission workflow:
  1. The custom properties of a submission (reflected in the submit.Submission
     db.model)
  2. The pre-submission handlers for an app
  3. The management actions for an app
  """
  app_name = db.StringProperty()


class SubmissionField(polymodel.PolyModel):
  """Extensible Model for a custom field of a submission

  Custom fields for a submission, such as submission type or submitter name,
  are implemented as Models extending this class, and are stored in a
  collection on the HotPotatoAppConfig entity.
  """
  app_config = db.ReferenceProperty(
      HotPotatoAppConfig,
      collection_name='submission_fields')
  field_name = db.StringProperty()

  def get_edit_form(self):
    """TODO docstring"""
    raise NotImplementedError
